const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    safeTop: '',
    is_settle: 2,
    starttime: '',
    endtime: '',
    list: [],
    finish: false,
    loading: false,
    page: 1,
    date: '',
    minDate: new Date(2023, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    show: false,
    sumPrice: '',
    token: wx.getStorageSync('token')
  },
  copy(event){
    wx.setClipboardData({
      data: event.currentTarget.dataset.content,
      success (res) {
        util.toast('已复制')
      }
    })
  },

  detail(e){
    util.skip('/pages/orderDetail/orderDetail?order_id='+e.currentTarget.dataset.id)
  },
  onConfirm(event) {
   
    const [start, end] = event.detail;
    let starttime = new Date(start.toLocaleDateString()).getTime(); 
    let endtime = new Date(end.toLocaleDateString()).getTime()+24*3600*1000 -1000;
    this.setData({
      show: false,
      starttime: starttime/1000,
      endtime: endtime/1000,
      date: `${util.formatTime(starttime,'yyyy/mm/dd')} - ${util.formatTime(endtime,'yyyy/mm/dd')}`,
    });
    this.reload()
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  selectDay() {
    this.setData({
      show: true
    })
  },
  cutTab(e) {
    this.setData({
      is_settle: e.currentTarget.dataset.tab
    })
    this.reload()
  },
  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.name}`,
      icon: 'none',
    });
  },
  reload() {
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more() {
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList() {
    if (!wx.getStorageSync('token')) return
    if (this.data.finish) {
      return
    }
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
      is_settle: this.data.is_settle,

    }
    if (this.data.starttime) {
      data['starttime'] = this.data.starttime
    }
    if (this.data.endtime) {
      data['endtime'] = this.data.endtime
    }
    http.get('order/shopsettleorder', data).then(res => {
      if (res.data.list.length === 0) {
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data.list)
      this.setData({
        list: arr,
        sumPrice: res.data.sumPrice
      })

    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let  currentDate = new Date(new Date().toLocaleDateString()).getTime(); // 获取当前日期
    let end = currentDate+24*3600*1000 -1000;
    let start = currentDate - 24*3600*1000*30
    this.setData({
      starttime: start/1000,
      endtime: end/1000,
      date: `${util.formatTime(start,'yyyy/mm/dd')} - ${util.formatTime(end,'yyyy/mm/dd')}`
    })

    this.setData({
      safeTop: app.globalData.safeTop+ 'px'
    })


  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.reload()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})